package com.taokatao.cardmee.business.map;

import android.os.Bundle;
import android.text.TextUtils;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.taokatao.cardmee.BaseActivity;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.common.widget.TitleLayout;

import butterknife.BindView;
import butterknife.ButterKnife;


public class AMapActivity extends BaseActivity {
    @BindView(R.id.l_title) TitleLayout lTitle;
    @BindView(R.id.v_map) MapView mMapView;
    AMap aMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSupportTouchBack = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_amap);
        ButterKnife.bind(this);
        mMapView.onCreate(savedInstanceState);
        aMap = mMapView.getMap();

        String title = getIntent().getStringExtra("extra_title");
        lTitle.setTitleText(title);

        double myLat = getIntent().getDoubleExtra("extra_my_lat", 39.906901);
        double myLng = getIntent().getDoubleExtra("extra_my_lng", 116.397972);
        LatLng myLatLng = new LatLng(myLat, myLng);
        aMap.addMarker(new MarkerOptions()
                .position(myLatLng)
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.map_self))
                .title("我的位置"));

        double targetLat = getIntent().getDoubleExtra("extra_target_lat", 39.906901);
        double targetLng = getIntent().getDoubleExtra("extra_target_lng", 116.397972);
        String targetTitle = getIntent().getStringExtra("extra_target_title");
        String targetAddress = getIntent().getStringExtra("extra_target_address");
        LatLng targetLatLng = new LatLng(targetLat, targetLng);
        Marker targetMarker = aMap.addMarker(new MarkerOptions()
                .position(targetLatLng)
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.map_target))
                .title(TextUtils.isEmpty(targetTitle) ? "目标商家" : targetTitle)
                .snippet(targetAddress));
        targetMarker.showInfoWindow();

        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLatLng, 12));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

}
